﻿using RsaRegister.Model;
using SoftRegManager.BLL;
using SoftRegManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SoftRegManager.Forms
{
    public partial class FrmSoftwareInfoes : Form
    {
        BindingList<SoftInfo> mSoftInfoes;
        GreatBLL mBLL;
        public FrmSoftwareInfoes(GreatBLL greatBLL)
        {
            InitializeComponent();
            mBLL = greatBLL;
        }
       
        private void FrmSoftwareInfoes_Load(object sender, EventArgs e)
        {
            dgvData.AutoGenerateColumns = false;
            IList<SoftInfo> apps = mBLL.GetSoftwareInfoes().ToList();
            mSoftInfoes = new BindingList<SoftInfo>(apps);
            dgvData.DataSource = mSoftInfoes;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void cmnAddSoftInfo_Click(object sender, EventArgs e)
        {
            FrmSoftInfoEdit frm = new FrmSoftInfoEdit(mBLL);
            if(frm.ShowDialog()== DialogResult.OK)
            {
                mSoftInfoes.Add(frm.SoftInfo);
            }
        }

        private void cmnEditSoftInfo_Click(object sender, EventArgs e)
        {
            if (dgvData.CurrentRow.Index < 0) return;
            SoftInfo softInfo = mSoftInfoes[dgvData.CurrentRow.Index];
            FrmSoftInfoEdit frm = new FrmSoftInfoEdit(mBLL, softInfo);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                mSoftInfoes[dgvData.CurrentRow.Index] = frm.SoftInfo;
            }
        }

        private void cmnDeleteSoftInfo_Click(object sender, EventArgs e)
        {
           if(MessageBox.Show("删除该软件，意味将不再为该软件提供续授权服务!!\r\n确定码？", "严重警告",
                MessageBoxButtons.YesNo,MessageBoxIcon.Warning)== DialogResult.Yes)
            {
                try
                {
                    mBLL.DeleteSoftwareInfo(mSoftInfoes[dgvData.CurrentRow.Index].SoftId);
                    mSoftInfoes.RemoveAt(dgvData.CurrentRow.Index);
                }
                catch (Exception ex)
                {

                    MessageBox.Show("错误信息为：\r\n" + ex.Message, "删除软件信息时发生异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
               
            }
        }

        private void dgvData_DoubleClick(object sender, EventArgs e)
        {
            
            cmnEditSoftInfo_Click(sender, e);
        }
    }
}
